package com.zaaach.citypicker.db;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zaaach.citypicker.model.City;
import com.zaaach.citypicker.util.ScreenUtil;
import ohos.app.Context;
import ohos.data.DatabaseHelper;
import ohos.data.orm.OrmContext;
import ohos.data.rdb.RdbOpenCallback;
import ohos.data.rdb.RdbStore;
import ohos.data.rdb.StoreConfig;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static com.zaaach.citypicker.db.DBConfig.COLUMN_C_CODE;
import static com.zaaach.citypicker.db.DBConfig.COLUMN_C_NAME;
import static com.zaaach.citypicker.db.DBConfig.COLUMN_C_PINYIN;
import static com.zaaach.citypicker.db.DBConfig.COLUMN_C_PROVINCE;
import static com.zaaach.citypicker.db.DBConfig.LATEST_DB_NAME;
import static com.zaaach.citypicker.db.DBConfig.TABLE_NAME;

/**
 * Author Bro0cL on 2016/1/26.
 */
public class DBManager {

    private Context mContext;

    public DBManager(Context context) {
        this.mContext = context;
    }

    public List<City> getData() {
        ohos.global.resource.ResourceManager resManager = mContext.getResourceManager();
        ohos.global.resource.RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/china_city.json");
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    rawFileEntry.openRawFile()));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
//        System.out.println(stringBuilder.toString());
        Gson gson = new Gson();
        List<City> city = gson.fromJson(stringBuilder.toString(), new TypeToken<List<City>>() {
        }.getType());
        return city;
    }

    public List<City> searchCity(final String keyword) {
        ohos.global.resource.ResourceManager resManager = mContext.getResourceManager();
        ohos.global.resource.RawFileEntry rawFileEntry = resManager.getRawFileEntry("resources/rawfile/china_city.json");
        //将json数据变成字符串
        StringBuilder stringBuilder = new StringBuilder();
        try {
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    rawFileEntry.openRawFile()));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        Gson gson = new Gson();
        List<City> city = gson.fromJson(stringBuilder.toString(), new TypeToken<List<City>>() {
        }.getType());
        List<City> result = new ArrayList<>();
        int length = keyword.length();
        for (City bean : city) {
            if (bean.getName().contains(keyword)) {
                City c = new City(bean.getName(), bean.getProvince(), bean.getPinyin(), bean.getCode());
                result.add(c);
            }
        }
        if (result.size()==0){
            for (City bean : city) {
                if (bean.getPinyin().length() >= length) {
                    String substring = bean.getPinyin().substring(0, length);
                    if (substring.equals(keyword)) {
                        City c = new City(bean.getName(), bean.getProvince(), bean.getPinyin(), bean.getCode());
                        result.add(c);
                    }
                }
            }
        }
        return result;
    }
}
